package com.geovis.emergency.spd.controller.basic;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.basic.service.IBasicShareDocService;
import com.geovis.emergency.spd.biz.basic.service.IBasicShareFolderService;
import com.geovis.emergency.spd.entity.basic.entity.BasicShareFolder;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicShareFolderMoveDTO;
import com.geovis.emergency.spd.entity.basic.pojo.dto.BasicShareFolderSaveDTO;
import com.geovis.emergency.spd.entity.pc.pojo.enums.StatusEnum;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Set;

/**
 * <p>
 * 基础模块-共享文档目录 前端控制器
 * </p>
*
* @author Lonsho
* @since 2022-07-12
*/
@RestController
@RequestMapping("/pc/basicShareFolder")
@RequiredArgsConstructor
@Api(value = "基础模块-共享文档目录接口", tags = "基础模块-共享文档目录接口")
@Slf4j
@Validated
public class BasicShareFolderController extends BaseController<IBasicShareFolderService> {
    private final IBasicShareDocService basicShareDocService;
    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = SwaggerConstants.SAVE_VALUE, notes = SwaggerConstants.SAVE_NOTES)
    public Result save(@Valid @RequestBody BasicShareFolderSaveDTO saveDTO) {
        BasicShareFolder basicShareFolder = BeanUtil.toBean(saveDTO,BasicShareFolder.class);
        if (ObjectUtil.isNull(basicShareFolder.getFolderId())){
            BasicShareFolder rootFolder = basicShareDocService.getOrCreateRootFolder(saveDTO.getPcDrillId());
            basicShareFolder.setFolderId(rootFolder.getId());
            basicShareFolder.setIsRoot(StatusEnum.FALSE.getCode());
        }
        baseService.save(basicShareFolder);
        return Result.ok();
    }
    @PostMapping("/update")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = SwaggerConstants.UPDATE_VALUE, notes = SwaggerConstants.UPDATE_NOTES)
    public Result update(@Valid @RequestBody BasicShareFolderSaveDTO basicShareFolderSaveDTO) {
        BasicShareFolder basicShareFolder = BeanUtil.toBean(basicShareFolderSaveDTO,BasicShareFolder.class);
        baseService.updateById(basicShareFolder);
        return Result.ok();
    }
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = SwaggerConstants.REMOVE_BY_ID_LIST_VALUE, notes = SwaggerConstants.REMOVE_BY_ID_LIST_NOTES)
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody(required = false) Set<String> idList) {
        baseService.removeByIdList(idList);
        return Result.ok();
    }
    @PostMapping("/move")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "移动文件夹", notes = "移动文件夹")
    public Result move(@Valid @RequestBody BasicShareFolderMoveDTO moveDTO) {
        BasicShareFolder basicShareFolder = baseService.getById(moveDTO.getId());

        BeanUtil.copyProperties(moveDTO,basicShareFolder);
        if (ObjectUtil.isNull(basicShareFolder.getFolderId())){
            BasicShareFolder rootFolder = basicShareDocService.getOrCreateRootFolder(basicShareFolder.getPcDrillId());
            basicShareFolder.setFolderId(rootFolder.getId());
        }
        baseService.updateById(basicShareFolder);
        return Result.ok();
    }
}
